package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> pageQuery(String name, Integer status, Integer pageNum, Integer pageSize) {
        //pagehelper分页 limit
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper查询
        Page<NursingProjectVo> page = nursingProjectMapper.pageQuery(name, status);
        //封装结果返回
        return PageResponse.of(page, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto dto) {
        //dto转entity
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        //校验护理项目名称是否存在 如果存在则抛异常
        Integer count = nursingProjectMapper.selectByNursingProjectName(dto.getName());
        if(count>0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_EXIST);
        }
        //调用mapper新增护理项   新增和修改时  必须使用entity实体类  不能用dto
        nursingProjectMapper.insertNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectNursingProjectById(id);
        //entity转vo
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }
    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        //dto转entity
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        //校验护理项目名称是否存在 如果存在则抛异常  select count(*) from nursing_project where name = #{name} and id != #{id}
        Integer count = nursingProjectMapper.selectByNursingProjectNameForUpdate(dto.getName(), dto.getId());
        if(count>0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_EXIST);
        }
        //调用mapper修改护理项目
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void deleteById(Long id) {

        //如果护理项目是启用状态 也不能删除
        NursingProject nursingProject = nursingProjectMapper.selectNursingProjectById(id);
        if(nursingProject==null){
            throw new BaseException(BasicEnum.NURSING_PROJECT_NOT_EXIST);
        }
        if(nursingProject.getStatus()==1){
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE_FAIL);
        }
        //调用mapper删除护理项目
        nursingProjectMapper.deleteNursingProjectById(id);
    }
}
